﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyLocalChurch.Business;
using MyLocalChurch.Constants;
using MyLocalChurch.Models;
using MyLocalChurch.Models.Enumerations;
using MyLocalChurch.Resources;
using RKConsulting.Helpers;
using RKConsulting.Web.Helpers;

namespace MyLocalChurch.KidsChurch.Reports
{
    public partial class ReportDetails : System.Web.UI.Page
    {
        private readonly MyContext _context = MyContextSingleton.Instance;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack)
                return;

            InitializeFilters();
            ShowReportDetails();
        }

        protected void InitializeFilters()
        {
            ddlCategories.BindEnumValues<ReportCategory>();
            var category = Request.QueryString[QueryParameters.Category];
            if (!string.IsNullOrEmpty(category))
                ddlCategories.SelectByText(category);

            var churchServices = new List<string> {MyResource.DropDownListAll};
            churchServices.AddRange(_context.ChurchServices
                                            .Where(cs => cs.IsActive)
                                            .Select(cs => cs.StartTime));

            ddlChurchServices.DataSource = churchServices;
            ddlChurchServices.DataBind();

            var churchServiceId = QueryHelper.GetNullableIntQueryParam(QueryParameters.ChurchServiceId);
            if (churchServiceId.HasValue)
            {
                var churchService = _context.ChurchServices.FirstOrDefault(cs => cs.Id == churchServiceId.Value);
                if (churchService != null)
                    ddlChurchServices.SelectedValue = churchService.StartTime;
            }
        }

        protected void ShowReportDetails()
        {
            var category = EnumHelper.Parse<ReportCategory>(ddlCategories.SelectedValue);
            var startTime = ddlChurchServices.SelectedItem.Text;
            var churchService = _context.ChurchServices.FirstOrDefault(cs => cs.StartTime == startTime);
            var startDate = dpStartDate.SelectedDate;
            var endDate = dpEndDate.SelectedDate;
            var checkedOutOnly = chkCheckedOutOnly.Checked;

            var reportDetails = ReportManager.RetrieveReportDetails(
                _context, churchService, category, startDate, endDate, checkedOutOnly);
            lblTotalRows.Text = reportDetails.Count.ToString();
            rptReportDetails.DataSource = reportDetails;
            rptReportDetails.DataBind();
        }

        protected void btnShowDetails_Click(object sender, EventArgs e)
        {
            ShowReportDetails();
        }

        protected void dpStartDate_SelectedDateChanged(object sender, EventArgs e)
        {
            dpEndDate.MinDate = dpStartDate.SelectedDate;
        }

        protected void dpEndDate_SelectedDateChanged(object sender, EventArgs e)
        {
            dpStartDate.MaxDate = dpEndDate.SelectedDate;
        }
    }
}